package com.chenx.gameview;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.R.integer;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.TextView;

import com.chenx.dafeiji.R.drawable;
import com.chenx.gameinterface.GameImage;

public class DaFeiJiGameView extends SurfaceView implements
		SurfaceHolder.Callback, Runnable, android.view.View.OnTouchListener
{
	private SurfaceHolder holder = null;

	private TextView fenDisplay = new TextView(getContext());
	private int fenShu = 0;
	private FeJiImage selectFeJiImage;
	private Bitmap baozha;// 爆炸
	private Bitmap diren;// 敌人
	private Bitmap me;// 我
	private Bitmap bg;// 背景
	private Bitmap zidan;// 子弹

	private SoundPool pool;
	private int sound_shot = 0;//
	private int sound_gameover = 0;//
	private int sound_bomb = 0;//

	private Bitmap mainBitmap;// 二级缓存

	private int display_w;// 屏幕宽高
	private int display_h;
	private boolean GameState = false;// 游戏状态

	private ArrayList<ZiDanImage> zidans = new ArrayList();
	private ArrayList<GameImage> gameImages = new ArrayList();

	private ArrayList<GameImage> gameBakImages = new ArrayList();

	public DaFeiJiGameView(Context context)
	{
		super(context);
		// TODO Auto-generated constructor stub
		getHolder().addCallback(this);
		setOnTouchListener(this);// 事件注册
	}

	private void GameInit()
	{
		// 加载资源
		baozha = BitmapFactory.decodeResource(getResources(), drawable.baozai);
		bg = BitmapFactory.decodeResource(getResources(), drawable.bg);
		zidan = BitmapFactory.decodeResource(getResources(), drawable.myzidan);
		diren = BitmapFactory.decodeResource(getResources(), drawable.diren);
		me = BitmapFactory.decodeResource(getResources(), drawable.me);

		// 二级缓存
		mainBitmap = Bitmap
				.createBitmap(display_w, display_h, Config.ARGB_8888);

		gameImages.add(new BeiJingImage(bg, display_w, display_h));
		gameImages.add(new FeJiImage(me, display_w, display_h));
		gameImages.add(new DiJiImage(diren, display_w, display_h, gameImages,
				baozha));

		// 加载声音
		pool = new SoundPool(3, AudioManager.STREAM_SYSTEM, 0);

		sound_bomb = pool.load(getContext(), drawable.bomb, 1);
		sound_gameover = pool.load(getContext(), drawable.gameover, 1);
		sound_shot = pool.load(getContext(), drawable.shot, 1);

	}

	// 绘画中心方法
	@SuppressWarnings("unchecked")
	@Override
	public void run()
	{
		try
		{
			Paint mPaint=new Paint();
			Paint p = new Paint();
			Random ran = new Random();
			int diren_num = 0;
			int zidan_num = 0;
			while (GameState)
			{
				for (GameImage game : gameImages)
				{
					if (game instanceof FeJiImage)
					{
						FeJiImage feji = (FeJiImage) game;

						selectFeJiImage = feji;
					}
				}

				// 绘画自己
				if (selectFeJiImage != null)
				{
					// if (diren_num == 1)
					// {
					// pool.play(sound_shot, 1, 1, 1, 0, 1);

					zidans.add(new ZiDanImage(selectFeJiImage, zidan,
							display_w, display_h, zidans, 0));

					// zidans.add(new ZiDanImage(selectFeJiImage, zidan,
					// display_w, display_h, zidans));
					// diren_num = 0;
					// }
					// diren_num++;
				}

				Canvas newCanvas = new Canvas(mainBitmap);

				// 绘画所有
				for (GameImage image : (List<GameImage>) gameImages.clone())
				{

					if (image instanceof DiJiImage)
					{
						// 将子弹告诉敌机
						if (((DiJiImage) image).ShouDaoGongJi(zidans, pool,
								sound_bomb))
						{
							fenShu += 100;
						}
					}

				
					  //设置paint
			        mPaint.setStyle(Style.FILL);//实心
			        //mPaint.setColor(Color.BLUE);//黄色
			        //mPaint.setAlpha(50);//透明度15
			        mPaint.setColor(Color.GREEN);
			        mPaint.setTextSize(100.0f);
			        
					if (image.getBitmap() != null)
					{
						newCanvas.drawBitmap(image.getBitmap(), image.getX(),
								image.getY(), p);

						newCanvas.drawText(String.valueOf(fenShu), 50, 100, mPaint);

					}

				}

				// 绘画子弹
				for (GameImage image : (List<GameImage>) zidans.clone())
				{
					newCanvas.drawBitmap(image.getBitmap(), image.getX(),
							image.getY(), p);
				}

				diren_num += ran.nextInt(10);
				if (diren_num >= 10)
				{
					gameImages.add(new DiJiImage(diren, display_w, display_h,
							gameImages, baozha));
					diren_num = 0;
				}

				Canvas canvas = holder.lockCanvas();
				canvas.drawBitmap(mainBitmap, 0, 0, p);
				holder.unlockCanvasAndPost(canvas);

			}
		} catch (Exception e)
		{
			Log.e("APP.TAG", "异常", e);
		}

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder)
	{
		// TODO Auto-generated method stub
		this.holder = holder;
		GameState = true;

	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height)
	{
		// TODO Auto-generated method stub
		if (gameBakImages.size() > 0)
		{
			gameImages = (ArrayList<GameImage>) gameBakImages.clone();

		} else
		{
			display_w = width;
			display_h = height;
			GameInit();
		}

		new Thread(this).start();

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder)
	{
		// TODO Auto-generated method stub
		gameBakImages = (ArrayList<GameImage>) gameImages.clone();
		gameImages.clear();
		GameState = false;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event)
	{
		if (event.getAction() == MotionEvent.ACTION_DOWN)
		{
			for (GameImage game : gameImages)
			{
				if (game instanceof FeJiImage)
				{
					FeJiImage feji = (FeJiImage) game;

					if (feji.getX() < event.getX()
							&& feji.getY() < event.getY()
							&& feji.getX() + feji.getHeight() > event.getX()
							&& feji.getY() + feji.getHeight() > event.getY())
					{
						selectFeJiImage = feji;
						Log.i("APP.TAG", "我被选中了");
					} else
					{
						selectFeJiImage = null;
						Log.i("APP.TAG", "我没有被选中了");
					}

					break;
				}
			}
		} else if (event.getAction() == MotionEvent.ACTION_MOVE)
		{
			if (selectFeJiImage != null)
			{
				selectFeJiImage.setX((int) event.getX()
						- selectFeJiImage.getWidth() / 2);
				selectFeJiImage.setY((int) event.getY()
						- selectFeJiImage.getHeight() / 2);
			}
		} else if (event.getAction() == MotionEvent.ACTION_UP)
		{
			selectFeJiImage = null;
		}
		return true;
	}

}
